package com.example.springboot.service;

import com.example.springboot.dto.CarouselImageDTO;
import com.example.springboot.entity.CarouselImage;
import com.example.springboot.mapper.CarouselImageMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CarouselImageService {
    private static final Logger logger = LoggerFactory.getLogger(CarouselImageService.class);

    @Autowired
    private CarouselImageMapper carouselImageMapper;

    @Autowired
    private OSSService ossService;

    public List<CarouselImage> getActiveCarouselImages() {
        logger.info("获取所有启用的轮播图");
        return carouselImageMapper.findActiveImages();
    }

    public List<CarouselImage> getAllCarouselImages() {
        logger.info("获取所有轮播图（包括禁用状态）");
        return carouselImageMapper.findAll();
    }

    public CarouselImage createCarouselImage(CarouselImageDTO dto, MultipartFile imageFile) throws Exception {
        CarouselImage carouselImage = new CarouselImage();
        carouselImage.setTitle(dto.getTitle());
        carouselImage.setLink(dto.getLink());
        carouselImage.setSortOrder(dto.getSortOrder() != null ? dto.getSortOrder() : 0);
        carouselImage.setIsActive(dto.getIsActive() != null ? dto.getIsActive() : true);
        carouselImage.setCreatedAt(Timestamp.valueOf(LocalDateTime.now()));
        carouselImage.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));

        if (imageFile != null && !imageFile.isEmpty()) {
            String imageUrl = ossService.uploadImage(imageFile);
            carouselImage.setImageUrl(imageUrl);
        } else {
            throw new IllegalArgumentException("轮播图图片不能为空");
        }

        carouselImageMapper.insert(carouselImage);
        logger.info("成功创建轮播图，ID: {}", carouselImage.getCarouselId());
        return carouselImage;
    }

    public CarouselImage updateCarouselImage(Integer carouselId, CarouselImageDTO dto, MultipartFile imageFile) throws Exception {
        CarouselImage carouselImage = carouselImageMapper.findById(carouselId);
        if (carouselImage == null) {
            logger.warn("轮播图未找到，ID: {}", carouselId);
            throw new IllegalArgumentException("轮播图不存在");
        }

        carouselImage.setTitle(dto.getTitle());
        carouselImage.setLink(dto.getLink());
        carouselImage.setSortOrder(dto.getSortOrder() != null ? dto.getSortOrder() : carouselImage.getSortOrder());
        carouselImage.setIsActive(dto.getIsActive() != null ? dto.getIsActive() : carouselImage.getIsActive());
        carouselImage.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));

        if (imageFile != null && !imageFile.isEmpty()) {
            String imageUrl = ossService.uploadImage(imageFile);
            carouselImage.setImageUrl(imageUrl);
        }

        carouselImageMapper.update(carouselImage);
        logger.info("成功更新轮播图，ID: {}", carouselId);
        return carouselImage;
    }

    public void deleteCarouselImage(Integer carouselId) {
        CarouselImage carouselImage = carouselImageMapper.findById(carouselId);
        if (carouselImage == null) {
            logger.warn("轮播图未找到，ID: {}", carouselId);
            throw new IllegalArgumentException("轮播图不存在");
        }

        carouselImageMapper.delete(carouselId);
        logger.info("成功删除轮播图，ID: {}", carouselId);
    }
}